package com.tboss.template.system.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import com.tboss.global.utils.useful.SecurityUtils;
import com.tboss.template.common.dto.NavigationDto;
import com.tboss.template.common.utils.DateUtils;
import com.tboss.template.system.dao.CodeDao;
import com.tboss.template.system.dao.CodeTypeDao;
import com.tboss.template.system.dto.CodeDto;
import com.tboss.template.system.dto.CodeTypeDto;

@Service
public class CodeService {
	
	@Autowired SystemService systemService;
	@Autowired CodeTypeDao codeTypeDao;
	@Autowired private CodeDao cdDao;
	
	private List<CodeTypeDto> getCodeTypeList(){
		CodeTypeDto codeTypeDto = new CodeTypeDto();
		codeTypeDto.setPagingYn("N");
		
		return this.codeTypeDao.getCodeTypeList(codeTypeDto);
	}
	
	private Integer getCodeListCnt(CodeDto dto){
		return this.cdDao.getCodeListCnt(dto);
	}
	
	private List<CodeDto> getCodeList(CodeDto dto){
		return this.cdDao.getCodeList(dto);
	}
	
	public void setModelCodeList(Integer reqPage, Model model, CodeDto dto){
		if(reqPage != null) dto.setCurrentPage(reqPage);
		
		Integer rowsCnt = this.getCodeListCnt(dto);
		NavigationDto pageNavi = new NavigationDto(rowsCnt, dto.getRowPerPage(), dto.getCurrentPage());
		
		model.addAttribute("sysBase", this.systemService.getSysBase(dto));
		model.addAttribute("codeTypes", this.getCodeTypeList());
		model.addAttribute("pageNavi", pageNavi);
		model.addAttribute("cdList", this.getCodeList(dto));
	}
	
	public void getCodeRegForm(Model model){
		model.addAttribute("codeTypes", this.getCodeTypeList());
	}

	public void getCodeRegForm(Model model, String cdId){
		model.addAttribute("codeTypes", this.getCodeTypeList());
		model.addAttribute("cdInfo", this.cdDao.getCodeInfo(cdId));
	}

	public void addCodeInfo(CodeDto dto){
		dto.setCdRegId(SecurityUtils.getCurrentUserName());
		dto.setCdRegDate(DateUtils.getDateFormat("yyyyMMddHHmmss"));
		this.cdDao.addCodeInfo(dto);
	}
	
	public void updateCodeInfo(CodeDto dto){
		dto.setCdModId(SecurityUtils.getCurrentUserName());
		dto.setCdModDate(DateUtils.getDateFormat("yyyyMMddHHmmss"));
		this.cdDao.updateCodeInfo(dto);
	}

}
